package com.work.csms.controller;

import com.work.csms.dao.*;
import com.work.csms.pojo.PageParam;
import com.work.csms.service.MoneyService;
import com.work.csms.service.OrderService;
import com.work.csms.service.StationService;
import com.work.csms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("/order")
@CrossOrigin(originPatterns = "*", allowCredentials = "true")
public class OrderController {

    @Autowired(required = false)
    private OrderService orderService;
    @Autowired(required = false)
    private UserService userService;
    @Autowired(required = false)
    private MoneyService moneyService;
    @Autowired(required = false)
    private StationService stationService;

    @PostMapping("/page")
    public Map<String, Object> page(@RequestBody Order order, @RequestBody PageParam pageParam) throws Exception {
        return orderService.page(order, pageParam);
    }

    @RequestMapping("/list")
    public List<Order> list(@RequestBody Order order) throws Exception {
        return orderService.list(order);
    }

    @PostMapping("/getOrderMoney")
    public Integer getOrderMoney(@RequestBody Order order) throws Exception {
        return orderService.getOrderMoney(order);
    }

    @RequestMapping("/getOrderById")
    public Order getOrderById(@RequestBody Order order) throws Exception {
        Integer id = order.getId();
        if (id == null) {
            return null;
        } else {
            return orderService.getOrderById(id);
        }
    }

    @RequestMapping("/saveOrUpdate")
    public Map<String, Object> saveOrUpdate(@RequestBody Order order) throws Exception {
        Map<String, Object> result = new HashMap<>();
        Integer id = order.getId();
        if (Objects.isNull(id)) {
            User byId = userService.getUserById(order.getUserid());
            if (byId.getMoney() != null && byId.getMoney() <= 0) {
                BigDecimal bigDecimal = new BigDecimal(byId.getMoney());
                result.put("msg", "已经欠费" + bigDecimal.divide(new BigDecimal(100)) + "元，请先充值");
                return result;
            }
            order.setStart(new Date());
            orderService.save(order);
            Station station = new Station();
            station.setId(order.getStationid());
            station.setStatu("充电中");
            stationService.update(station);
        } else {
            order.setEnd(new Date());
            orderService.update(order);
        }
        if (order.getMoney() != null) {
            userService.updateMoneyByUserid(order.getUserid(), order.getMoney());
            Money money = new Money();
            money.setMoney(order.getMoney());
            money.setType("消费");
            money.setOrderid(order.getId());
            money.setUserid(order.getUserid());
            money.setDate(new Date());
            moneyService.save(money);
            Station station = new Station();
            station.setId(order.getStationid());
            station.setStatu("正常");
            stationService.update(station);
        }
        result.put("success", true);
        return result;
    }

    @RequestMapping("/stop")
    public Map<String, Object> stop(@RequestBody Order order) throws Exception {
        Map<String, Object> result = new HashMap<>();
        order.setStatu("进行中");
        List<Order> list = list(order);
        if (list == null || list.size() == 0) {
            result.put("msg", "暂无正在使用的充电桩");
            return result;
        }
        Order one = list.get(0);
        order.setStatu("完成");
        order.setStationid(one.getStationid());
        order.setId(one.getId());
        Date end = new Date();
        Date start = one.getStart();
        // 计算时间差（毫秒）
        long timeDiff = end.getTime() - start.getTime();
        // 将时间差转换为分钟，并取整
        long minutes = timeDiff / (1000 * 60);
        order.setEnd(end);
        Integer money = - ((int) minutes * 5);  // 一分钟 5 分钱
        order.setMoney(money);
        saveOrUpdate(order);
        result.put("success", true);
        return result;
    }

    @RequestMapping("/deleted")
    public Map<String, Object> deleted(@RequestBody Order order) throws Exception {
        Map<String, Object> result = new HashMap<>();
        Integer id = order.getId();
        if (Objects.isNull(id)) {
            result.put("msg", "订单id不存在");
        } else {
            orderService.deleted(order.getId());
            result.put("success", true);
        }
        return result;
    }
}
